#jinja2: lstrip_blocks: True

{% for service in apache_services %}
Listen {{ ip.mgmt }}:{{ service.port }}
{% endfor %}

{% for service in apache_services %}
<VirtualHost *:{{ service.port }}>

{% if service.include is defined %}
    Include {{ service.include }}
{% endif %}

    ErrorLog ${APACHE_LOG_DIR}/error.log
    CustomLog ${APACHE_LOG_DIR}/{{ apache_service_name }}-access.log combined

{% if apache_service_ssl | bool %}
    SSLEngine on
    SSLCertificateFile      {{ SSLCertificateFile }}
    SSLCertificateKeyFile   {{ SSLCertificateKeyFile }}
    {% if apache_configure_certificate_chain | bool %}
    # This is obsolete from Apache 2.4.8
    SSLCertificateChainFile {{ SSLCertificateFile }}
    {% endif %}
    {% if SSLCACertificateFile != '' %}
    SSLCACertificateFile    {{ SSLCACertificateFile }}
    {% endif %}
{% endif %}

{% if apache_proxy_preserve_http_auth | bool %}
    RewriteEngine On
    RewriteRule .* - [E=HTTP_AUTHORIZATION:%{HTTP:Authorization},L]
{% endif %}

{% if apache_proxy_nokeepalive | bool %}
    SetEnv proxy-nokeepalive 1
{% endif %}

{% if service.wsgi_process is defined %}
    WSGIDaemonProcess       {{ service.wsgi_process }} processes={{ service.wsgi_process_count | default((ansible_processor_vcpus + 1) // 2) }} threads=1 user={{ service.wsgi_user }} display-name=%{GROUP}
    WSGIProcessGroup        {{ service.wsgi_process }}
    WSGIScriptAlias /       {{ service.wsgi_script }}
    WSGIApplicationGroup    %{GLOBAL}
    WSGIPassAuthorization On
{% else %}
    ProxyPreserveHost       {{ apache_proxy_preserve_host | ternary('On', 'Off') }}
    ProxyPass /             {{ service.dest_proto | default('http') }}://127.0.0.1:{{ service.dest_port | default(service.port) }}/ retry=10
    ProxyPassReverse /      {{ service.dest_proto | default('http') }}://127.0.0.1:{{ service.dest_port | default(service.port) }}/ retry=10
{% endif %}

</VirtualHost>

{% endfor %}
